package cn.lu.service.impl;

import cn.lu.dao.ChapterMapper;
import cn.lu.dao.SectionMapper;
import cn.lu.pojo.Chapter;
import cn.lu.pojo.Section;
import cn.lu.service.SectionService;
import cn.lu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SectionServiceImpl implements SectionService {
    @Autowired
    private SectionMapper sectionMapper;

    @Autowired
    private ChapterMapper chapterMapper;

    @Override
    public int addInsert(Section section) {
        if (chapterMapper.selectByPrimaryKey(section.getChapterId())!= null){
            return sectionMapper.insert(section);
        }
        return 0;
    }

    @Override
    public List<Section> selectChapterId(Integer chapterId) {
        return sectionMapper.selectChapterId(chapterId);
    }

    @Override
    public int deleteByIdAndUserId(Integer sectionId, Integer userId) {
        return sectionMapper.deleteByIdAndUserId(sectionId,userId);
    }

    @Override
    public Result addInTheUserId(Section section, Integer userId) {
       if (chapterMapper.chapterWhetherTeacher(section.getChapterId(), userId) ==1) {
           if (sectionMapper.insert(section) == 1) {
               return new Result().success();
           } else {
               return new Result().fail();
           }
       } else {
           return new Result().fail("您没有权限操作");
       }
    }

    @Override
    public Result updateByIdAndUserId(Section section, Integer userId) {
        if (sectionMapper.updateByIdAndUserId(section, userId) == 1) {
            return new Result().success();
        } else {
            return new Result().fail();
        }
    }


}